package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.algorithm.digest.MD5;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.reflection.ClassConvertUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.http.org.entity.apply.OrgApplyDinXinHeLi;
import com.ts.api.http.org.entity.dataexchange.OrgDataExchangeDinXinHeLi;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;

/**
 *  鼎新合力
 *
 *  @author WJB
 */
@ApiPlan(productId = "36", type = ApiPlanConvertType.ORG_PHONE_MD5)
public class ApiOrgPhoneMd5DinXinHeLi implements ApiOrgPhoneMd5ConvertHandler {

    private static final String KEY = "GjL2tHwGPjD2gH8z";

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) {
        String timestamp = ClassConvertUtils.toStr(System.currentTimeMillis() / 1000);

        OrgDataExchangeDinXinHeLi data = new OrgDataExchangeDinXinHeLi();
        data.setTimestamp(timestamp);
        data.setCompany(1);
        data.setChannel_code("AXFQ");
        data.setMobile(apiDataExchangeParameter.getPhoneMd5());
        String sign = MD5.create().digestToHex(data.getChannel_code() + data.getMobile() + timestamp + KEY);
        data.setSign(sign);
        return data;
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        if (!Eq.str(jsonObject.getString("code"), "0")) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) {
        OrgApplyDinXinHeLi apply = new OrgApplyDinXinHeLi();
        apply.setName(apiApplyParameter.getUserName());
        apply.setMobile(apiApplyParameter.getPhone());
        apply.setCompany(1);
        //  不能包含市
        String city = apiApplyParameter.getCity();
        if (ValidParam.isNotNull(city)) {
            city = city.replace("市", "");
            apply.setCity(city);
        }
        //  车
        if (ValidParam.isNotEmpty(apiApplyParameter.getCarInfo())) {
            String car = ChooseEq.create()
                    .when(apiApplyParameter.getCarInfo(), "1", "无")
                    .end("有");
            apply.setCar(car);
        }

        //  房产情况
        if (ValidParam.isNotEmpty(apiApplyParameter.getRealEstateInfo())) {
            String house = ChooseEq.create()
                    .when(apiApplyParameter.getRealEstateInfo(), "1", "无")
                    .end("有");
            apply.setHouse(house);
        }

        //  保单
        if (ValidParam.isNotEmpty(apiApplyParameter.getInsurancePolicy())) {
            String baodanIs = ChooseEq.create()
                    .when(apiApplyParameter.getInsurancePolicy(), "1", "无")
                    .end("有");
            apply.setBaodan_is(baodanIs);
        }
        //  性别
        String sex = apiApplyParameter.getSex();
            String sexChoose = ChooseEq.create()
                    .when(sex, "0", "男")
                    .when(sex, "1", "女")
                    .end();
        apply.setSex(sexChoose);

        //  年龄
        ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);
        apply.setAge(apiFieldConverter.getAgeInt());

        //  贷款额度
        Integer money = ChooseEq.create()
            .when(apiApplyParameter.getLoanAmount(), "1", 3)
            .when(apiApplyParameter.getLoanAmount(), "2", 5)
            .when(apiApplyParameter.getLoanAmount(), "3", 10)
            .when(apiApplyParameter.getLoanAmount(), "4", 20)
        .end();
        apply.setMoney(money);

        // 贷款来源(固定值，具体需要和对接人确定)
        apply.setSource("AXFQ");
        //  社保
        if (ValidParam.isNotEmpty(apiApplyParameter.getSocialSecurity())) {
            String shebao = ChooseEq.create()
                .when(apiApplyParameter.getSocialSecurity(), "1", "无")
            .end("有");
            apply.setShebao(shebao);
        }

        //  公积金
        if (ValidParam.isNotEmpty(apiApplyParameter.getProvidentFund())) {
            String gongjijin = ChooseEq.create()
                    .when(apiApplyParameter.getProvidentFund(), "1", "无")
            .end("有");
            apply.setGongjijin(gongjijin);
        }

        //  银行代发
        if (ValidParam.isNotEmpty(apiApplyParameter.getSalaryPayMethod())) {
            String isbankpay = ChooseEq.create()
                .when(apiApplyParameter.getSalaryPayMethod(), "1", "是")
            .end("否");
            apply.setIsbankpay(isbankpay);
        }

        //  ip
        if (ValidParam.isNotEmpty(apiApplyParameter.getIp())) {
            apply.setIp(apiApplyParameter.getIp());
        }
        //  信用卡
        if (ValidParam.isNotEmpty(apiApplyParameter.getCreditLimit())) {
            String creditCard = ChooseEq.create()
                .when(apiApplyParameter.getCreditLimit(), "1", "无")
                .end("有");
            apply.setCredit_card(creditCard);
        }
        //  时间
        String timestamp = ClassConvertUtils.toStr(System.currentTimeMillis());
        timestamp = timestamp.substring(0, timestamp.length() - 3);
        apply.setTime(ClassConvertUtils.toInt(timestamp));

        return apply;
    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) {
        if (ValidParam.isNull(respJson)) {
            return RespOrgPhoneMd5Apply.ofFail();
        }

        int result = ClassConvertUtils.toInt(respJson);
        //  大于80000成功
        if ( result < 80000) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
